<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height: 60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="hGet"
                >新增角色</el-button
              >
            </el-row>
            <!-- 表格 -->
            <el-table :data="list">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="{ row }">
                  <el-button size="small" type="success" @click="hAll(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editor(row)"
                    >编辑</el-button
                  >
                  <el-button size="small" type="danger" @click="hDele(row.id)"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>

            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 -->
              <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="show.page"
                :page-sizes="[2, 5, 6, 49]"
                :page-size="show.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
              >
              </el-pagination>
            </el-row>
          </el-tab-pane>

          <!-- 新增弹框 -->
          <el-dialog title="编辑弹层" @close="resetForm" :visible.sync="showDialog">
            <el-form
              ref="roleForm"
              :model="roleForm"
              :rules="rules"
              label-width="100px"
            >
              <el-form-item label="角色名称" prop="name">
                <el-input v-model="roleForm.name" />
              </el-form-item>
              <el-form-item label="角色描述" prop="description">
                <el-input v-model="roleForm.description" />
              </el-form-item>
            </el-form>
            <!-- 底部 -->
            <el-row slot="footer" type="flex" justify="center">
              <el-col :span="6">
                <el-button size="small" type="primary" @click="hSubmit"
                  >确定</el-button
                >
                <el-button size="small">取消</el-button>
              </el-col>
            </el-row>
          </el-dialog>

          <!-- 弹框分配权限 -->

          <el-dialog 
    title="分配权限"  :visible.sync="showDialogAssign">
  <assign-permission v-if="showDialogAssign" @close="showDialogAssign=false"  :id="userId"/>
</el-dialog>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import assignPermission from './assignPermission'
import {
  getSttings,
  roleSttings,
  editorSttings,
  deleSttings,
} from "@/api/settings";
export default {
  components:{
    assignPermission
  },
  data() {
    return {
      showDialogAssign:false,//显示隐藏
      showDialog: false, //显示隐藏
      // 分页
      show: {
        page: 1,
        pagesize: 2,
      },

      // 收集参数
      roleForm: {
        name: "",
        description: "",
      },
      // 规则效验
      rules: {
        name: [{ required: true, message: "请输入角色名称", trigger: "blur" }],
        description: [
          { required: true, message: "请输入角色描述", trigger: "blur" },
        ],
      },
      list: [],
      total: 0,
      isUs: false, //变量
      userId:''
    };
  },
  created() {
    this.hAdd();
  },

  methods: {
    // 分配
    hAll(id){
      this.showDialogAssign=true
      this.userId=id
    },
    // 重置表单
    resetForm(){
      this.$refs.roleForm.resetFields()
      this.roleForm={
        name: "",
        description: "",
      }
    },
    // 删除
    hDele(id) {
      this.$confirm("是否确定删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const res = await deleSttings(id);
          // console.log(res);
          if (this.list.length === 1) {
            
            this.show.page--;
            if(this.show.page<=0){
              this.show.page=1
            this.hAdd();
            }
this.hAdd();
          }else{
            this.hAdd()
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 编辑角色列表
    async loadditorSttings() {
      try {
        const res = await editorSttings(this.roleForm);
        // console.log(res);
        this.hAdd();
        this.showDialog = false;
      } catch (error) {
        console.log(error);
      }
    },
    // 编辑按钮
    editor(data) {
      this.showDialog = true;
      this.isUs = true;
      this.roleForm = { ...data };
    },
    // 确定按钮
    hSubmit() {
      this.$refs.roleForm.validate((valid) => {
        if (!valid) return;
        this.total++;
        const aa = Math.ceil(this.total / this.show.pagesize);

        if (this.isUs) {
          this.loadditorSttings();
        } else {
          this.show.page = aa;
          this.hhGet();
        }
      });
    },
    // 添加
    async hhGet() {
      try {
        await roleSttings(this.roleForm);
        // console.log(res);
        this.hAdd();
        this.showDialog = false;
      } catch (error) {
        console.log(error);
      }
    },
    // 添加
    hGet() {
      this.isUs = false;
      this.showDialog = true;
    },
    // 分页
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.show.page = 1;
      this.show.pagesize = val;
      this.hAdd();
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.show.page = val;
      this.hAdd();
    },
    // 获取所有角色列表
    async hAdd() {
      try {
        const res = await getSttings(this.show);
        // console.log(res);
        this.list = res.data.rows;
        this.total = res.data.total;
      } catch (error) {
        console.log(error);
      }
    },
  },
};
</script>

<style>
</style>